package com.easy.easy.util;

import com.easy.easy.bean.User;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpSession;

public class SYS {
    //文件上传保存路径
    public static final String FILE_UPLOAD_DIR="D:\\easyupload\\";

    //上传文件访问前缀路径
    public static final String FILE_UPLOAD_URL="easyfile";

    public static final String SHOPPING_LOGIN_USER="SHOPPING_LOGIN_USER";

    public static final String SYSTEM_LOGIN_ADMIN="SYSTEM_LOGIN_ADMIN";

    public static int getLoginUserID(HttpSession session) {
        User user=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        return user.getUserid();
    }

    public static final int[] HEX_CHAR_ARR={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

    public static boolean check(String reg,String... params){
        boolean result=true;
        for(String item:params){
            if(item==null){
                return false;
            }
            if(item.matches(reg)){
                return false;
            }
        }
        return result;
    }

    public static String digest(String pass) {

        //简单加密 暴力破解
        //String digestPass= DigestUtils.md5DigestAsHex(pass.getBytes());
        //多次加密
//        for(int i=0;i<10;i++){
//          pass=DigestUtils.md5DigestAsHex(pass.getBytes());
//            System.out.println(pass);
//        }

        //盐值加密
//        String salt = "abc";
//        pass=DigestUtils.md5DigestAsHex((pass+salt).getBytes());
//        System.out.println(pass);

        //随机次数盐值加密
        int countIndex=(int)(Math.random()*HEX_CHAR_ARR.length);
        int saltIndex=(int)(Math.random()*HEX_CHAR_ARR.length);
        int count=HEX_CHAR_ARR[countIndex];
        char salt=(char)HEX_CHAR_ARR[saltIndex];
        char countChar=(char)count;
        pass=pass+salt;
        for(int i=0;i<count;i++){
            pass=DigestUtils.md5DigestAsHex(pass.getBytes());
        }


        String passwordText=salt+pass+countChar;
        return passwordText;

    }
    //登陆验证方法
    public static boolean checkPass(String pass,String hexPass){
        char salt=hexPass.charAt(0);//盐值
        char countChar=hexPass.charAt(hexPass.length()-1);//次数
        pass=pass+salt;
        for(int i=0;i<countChar;i++){
            pass=DigestUtils.md5DigestAsHex(pass.getBytes());
        }


        String text=salt+pass+countChar;
        return text.equals(hexPass);
    }




    public static void main(String[] args) {
        //QrCodeUtil generate();

    }
}
